package com.dankegongyu.lib.dialog;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

/**
 * @author wupuquan
 * @date 2020/8/28 19:00
 */
@SuppressWarnings({"unchecked", "UnusedReturnValue"})
public abstract class DKBaseBottomSheetDialog<T extends DKBaseBottomSheetDialog> extends BottomSheetDialog implements IDKDialog {

    private static final String TAG = DKBaseBottomSheetDialog.class.getSimpleName();

    private BottomSheetBehavior bottomSheetBehavior;
    private int width; // 宽度
    private int height; // 高度
    private float dimAmount = 0.5f; // 透明度
    private int peekHeight;
    private boolean skipCollapsed = true; // 跳过折叠，即下滑直接关闭
    private boolean expandDefault = true; // 是否默认展开

    public DKBaseBottomSheetDialog(@NonNull Context context) {
        super(context);
    }

    public DKBaseBottomSheetDialog(@NonNull Context context, int theme) {
        super(context, theme);
    }

    protected DKBaseBottomSheetDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public T width(int width) {
        this.width = width;
        return (T) this;
    }

    public T height(int height) {
        this.height = height;
        return (T) this;
    }

    public T peekHeight(int peekHeight) {
        this.peekHeight = peekHeight;
        return (T) this;
    }

    /**
     * 是否跳过折叠，即下滑直接关闭
     */
    public T setSkipCollapsed(boolean skipCollapsed) {
        this.skipCollapsed = skipCollapsed;
        return (T) this;
    }

    /**
     * 是否默认展开
     */
    public T setExpandDefault(boolean expandDefault) {
        this.expandDefault = expandDefault;
        return (T) this;
    }

    public T dimAmount(@FloatRange(from = 0.0, to = 1.0) float dimAmount) {
        this.dimAmount = dimAmount;
        return (T) this;
    }


    private boolean firstShow = true;

    @Override
    public void show() {
        if (firstShow) {
            handleFirstShow();
        }

        super.show();
        firstShow = false;

        if (bottomSheetBehavior != null && expandDefault) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); // 全展开
        }

        setWindowAttributes();
    }

    private void handleFirstShow() {
        int contentViewLayoutId = getContentViewLayoutId();
        if (contentViewLayoutId != 0) {
            View view = getLayoutInflater().inflate(contentViewLayoutId, null);
            setContentView(view);
            handleBottomSheet(view);
        }

        init();

        doBusiness();
    }

    private void handleBottomSheet(View view) {
        try {
            FrameLayout bottomSheet = (FrameLayout) view.getParent();

            try {
                bottomSheet.setBackgroundColor(Color.TRANSPARENT); // 关键代码！去掉默认白色背景
                int[] colors = new int[]{Color.TRANSPARENT};
                int[][] states = new int[1][];
                states[0] = new int[]{};
                ColorStateList colorList = new ColorStateList(states, colors);
                bottomSheet.setBackgroundTintList(colorList); // 主题是Theme.MaterialComponents时这个背景也需要处理
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (height > 0) {
                CoordinatorLayout.LayoutParams lpBS = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
                lpBS.height = height; // 实测发现WebDialog加载成功前高度为0，这里强制设置高度
            }
            bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
            if (peekHeight > 0) {
                bottomSheetBehavior.setPeekHeight(peekHeight);
            }
            bottomSheetBehavior.setSkipCollapsed(skipCollapsed);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setWindowAttributes() {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            // 调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = dimAmount;
//            if (width > 0) {
//                lp.width = width;
//            } else {
//                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
//            }
//            if (height > 0) {
//                lp.height = height;
//            } else {
//                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
//            }
//            lp.gravity = Gravity.BOTTOM;
            window.setAttributes(lp);
        }
    }

    public void destroy() {
        if (isShowing()) {
            dismiss();
        }
    }
}